#!/bin/bash

# 监控服务启动脚本
# 远程服务器信息
SERVER="root@182.160.6.140"
DEPLOY_PATH="/var/www/binance-analysis"
MONITOR_PORT=3002

echo "开始启动监控服务..."

# 先杀死可能占用监控服务端口的进程
ssh $SERVER "lsof -t -i:$MONITOR_PORT | xargs -r kill -9"
sleep 2

# 停止旧的监控服务
echo "停止旧的监控服务..."
ssh $SERVER "pm2 stop monitor-app || true"
ssh $SERVER "pm2 delete monitor-app || true"
sleep 1

# 再次检查端口是否被占用
echo "检查$MONITOR_PORT端口占用情况..."
ssh $SERVER "lsof -t -i:$MONITOR_PORT | xargs -r kill -9"
sleep 1

# 启动监控服务
echo "启动监控服务..."
ssh $SERVER "cd $DEPLOY_PATH && PORT=$MONITOR_PORT pm2 start scripts/monitor.js --name monitor-app"

# 保存PM2配置
ssh $SERVER "pm2 save"

# 显示运行状态
echo "显示服务运行状态..."
ssh $SERVER "pm2 list"

echo "监控服务启动操作已完成！" 